package view;

import javax.swing.*;

import view.panel.BillPanel;
import view.panel.CommodityPanel;
import view.panel.EmployeePanel;
import view.panel.ShopPanel;
import view.panel.UserPanel;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.SQLException;

public class MainFrame extends JFrame {
    private static final long serialVersionUID = 1589964243622961935L;
    private JPanel contentPane;
    private JPanel buttonPanel;
    private JButton billButton, commodityShopButton, commodityButton, employeeButton, userButton;
    private JPanel cardPanel;
    private JPanel billPanel, commodityShopPanel, commodityPanel, employeePanel, userPanel;
    private CardLayout cardLayout = new CardLayout();

    public MainFrame(Connection conn) throws SQLException, ClassNotFoundException {
        setTitle("商城");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(new BorderLayout());
        buttonPanel = new JPanel();
        billButton = new JButton("结账");
        billButton.setName("bill");
        commodityShopButton = new JButton("购物");
        commodityShopButton.setName("shop");
        commodityButton = new JButton("商品");
        commodityButton.setName("commodity");
        employeeButton = new JButton("员工");
        employeeButton.setName("employee");
        userButton = new JButton("用户");
        userButton.setName("user");
        buttonPanel.add(billButton);
        buttonPanel.add(commodityShopButton);
        buttonPanel.add(commodityButton);
        buttonPanel.add(employeeButton);
        buttonPanel.add(userButton);
        contentPane.add(buttonPanel, BorderLayout.NORTH);
        cardPanel = new JPanel(cardLayout);
        billPanel = new BillPanel(conn);
        commodityShopPanel = new ShopPanel(conn);
        commodityPanel = new CommodityPanel(conn);
        employeePanel = new EmployeePanel(conn);
        userPanel = new UserPanel(conn);
        cardPanel.add(billPanel);
        cardPanel.add(commodityShopPanel);
        cardPanel.add(commodityPanel);
        cardPanel.add(employeePanel);
        cardPanel.add(userPanel);
        cardLayout.addLayoutComponent(billPanel, billPanel.getName());
        cardLayout.addLayoutComponent(commodityShopPanel, commodityShopPanel.getName());
        cardLayout.addLayoutComponent(commodityPanel, commodityPanel.getName());
        cardLayout.addLayoutComponent(employeePanel, employeePanel.getName());
        cardLayout.addLayoutComponent(userPanel, userPanel.getName());
        contentPane.add(cardPanel, BorderLayout.CENTER);
        billButton.addActionListener(showPanel());
        commodityShopButton.addActionListener(showPanel());
        commodityButton.addActionListener(showPanel());
        employeeButton.addActionListener(showPanel());
        userButton.addActionListener(showPanel());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                if (((BillPanel) billPanel).cartHasCommodity()) {
                    JOptionPane.showMessageDialog(null, "请先清空购物车内的商品");
                } else {
                    int state = JOptionPane.showConfirmDialog(null, "是否退出系统？", "退出系统", JOptionPane.YES_NO_OPTION);
                    if (state == 0) {
                        System.exit(WindowEvent.WINDOW_CLOSING);
                    }
                }
            }
        });
    }

    private ActionListener showPanel() {
        return actionEvent -> {
            JButton button = (JButton) actionEvent.getSource();
            switch (button.getName()) {
            case "bill":
                cardLayout.show(cardPanel, billPanel.getName());
                break;
            case "shop":
                cardLayout.show(cardPanel, commodityShopPanel.getName());
                break;
            case "commodity":
                cardLayout.show(cardPanel, commodityPanel.getName());
                break;
            case "employee":
                cardLayout.show(cardPanel, employeePanel.getName());
                break;
            case "user":
                cardLayout.show(cardPanel, userPanel.getName());
                break;
            }
        };
    }
}
